/**
 * Created by woziji00226 on 2024-12-27 10:13:31.
 */
class GUI_EyesightGamePlay_W26 extends GUI_15001 {
    static EVENT_PLAY_TEXT_ANIMATION_COMPLETED = "EVENT_PLAY_TEXT_ANIMATION_COMPLETED";

    /**
     * 缓动类，用于给文字一个渐变效果
     */
    private _tween: Tween;

    private _lv: number = 1;
    private _hp: number = 3;

    private _witr = "猜猜金币在哪儿？（请选择）";
    private _fail = ["是不是眼花了？", "别瞎猜，看准了再选", "太快了吗？"];
    private _ok = ["这个太简单了", "还不错，不过这只是练习！", "你确定没有使诈？", "好", "再接再厉", "很好", "太棒了", "下一关会更难哦", "牛逼！", "不可思议", "太夸张了吧", "太厉害了有木有？？", "你是怎么做到的？！", "肯定是运气！", "你的眼力那么好，你家人知道吗？", "不会再让你过关的，小心哦！", "简直无法置信！", "从来没有人能到这一关", "大神，教教我你的诀窍吧！", "我不玩了！我不玩了！"];

    constructor() {
        super();

        this.lv = 0;
        this.hp = 3;

        this._tween = new Tween();

        var taskName = "同步任务1";

        new SyncTask(taskName, () => {
            // 第1个顺序任务逻辑
            this.playTextAnimation("准备好了吗？");
            this.once(GUI_EyesightGamePlay_W26.EVENT_PLAY_TEXT_ANIMATION_COMPLETED, this, () => {
                // 第1个顺序任务执行完毕
                SyncTask.taskOver(taskName);
            })
        });

        new SyncTask(taskName, () => {
            // 第1个顺序任务逻辑
            this.playTextAnimation("开始");
            this.once(GUI_EyesightGamePlay_W26.EVENT_PLAY_TEXT_ANIMATION_COMPLETED, this, () => {
                // 第1个顺序任务执行完毕
                SyncTask.taskOver(taskName);
            })
        });

        new SyncTask(taskName, () => {
            // 第1个顺序任务逻辑
            this.startNext(1);
            SyncTask.taskOver(taskName);
        });

    }

    private playTextAnimation(text: string) {
        this.hideMsgLabel();
        this.msgLabel.text = text;
        this._tween.clear();
        this._tween.to(this.msgLabel, { alpha: 1 }, 1000, Ease.linearIn, Callback.New(() => {
            this._tween.to(this.msgLabel, { alpha: 0 }, 1000, Ease.linearIn, Callback.New(() => {
                this.event(GUI_EyesightGamePlay_W26.EVENT_PLAY_TEXT_ANIMATION_COMPLETED);
            }, this))
        }, this));
    }


    private hideMsgLabel() {
        this.msgLabel.alpha = 0;
    }

    private showMsgLabel() {
        this.msgLabel.alpha = 1;
    }

    /**
     * 开启下一轮
     * @param ilv 增加的关卡
     */
    private startNext(ilv: number) {
        var taskName = "同步任务3";
        this.lv += ilv;
        this.group.setCoinToCup1();



        new SyncTask(taskName, () => {
            // 第1个顺序任务逻辑
            this.playTextAnimation(`开始关卡 ${this.lv}`);
            this.once(GUI_EyesightGamePlay_W26.EVENT_PLAY_TEXT_ANIMATION_COMPLETED, this, () => {
                SyncTask.taskOver(taskName);
            });
        });


        new SyncTask(taskName, () => {

            this.group.cup_1.open();
            this.group.cup_1.once(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, () => {
                Callback.New(() => {
                    //动画播放完毕后，进行随机交换
                    var count = 8 + Math.ceil(this.lv * 4);
                    var s = Math.floor(this.lv * 2);

                    this.group.speedTime -= s;
                    if (this.group.speedTime <= 100) {
                        this.group.speedTime = 100;
                    }

                    this.group.randomSwap(count);

                    this.group.once(GUI_CupGroup_W26.EVENT_SWAP_COMPLETED, this, () => {
                        this.playTextAnimation(this._witr);
                    })

                    this.group.cup_0.once(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCupCompleted, [this.group.cup_0]);
                    this.group.cup_1.once(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCupCompleted, [this.group.cup_1]);
                    this.group.cup_2.once(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCupCompleted, [this.group.cup_2]);

                    this.group.cup_0.once(Sprite_Cup_W26.EVENT_OPEN, this, this.onOpenCup, [this.group.cup_0]);
                    this.group.cup_1.once(Sprite_Cup_W26.EVENT_OPEN, this, this.onOpenCup, [this.group.cup_1]);
                    this.group.cup_2.once(Sprite_Cup_W26.EVENT_OPEN, this, this.onOpenCup, [this.group.cup_2]);
                }, this).delayRun(200, setTimeout);
            });

            SyncTask.taskOver(taskName);
        });
    }

    /**
     * 打开杯子的时候触发 盖住杯子
     * @param cup 
     */
    private onOpenCup(cup: Sprite_Cup_W26) {
        this.group.cup_0.off(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCup);
        this.group.cup_1.off(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCup);
        this.group.cup_2.off(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCup);

        this.group.maskCups();
    }

    /**
     * 播放完打开杯子动画时触发 
     * @param cup 
     */
    private onOpenCupCompleted(cup: Sprite_Cup_W26) {

        var taskName = "同步任务2";

        this.group.cup_0.off(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCupCompleted);
        this.group.cup_1.off(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCupCompleted);
        this.group.cup_2.off(Sprite_Cup_W26.EVENT_PLAY_OPEN_ANIMATION_COMPLETED, this, this.onOpenCupCompleted);

        this.group.maskCups();


        if (cup.isExistCoin) {

            new SyncTask(taskName, () => {
                // 第1个顺序任务逻辑
                var index = this.lv - 1;
                if (index > this._ok.length - 1) {
                    index = this._ok.length - 1;
                }

                this.playTextAnimation(this._ok[index]);
                this.once(GUI_EyesightGamePlay_W26.EVENT_PLAY_TEXT_ANIMATION_COMPLETED, this, () => {
                    this.startNext(1);
                    SyncTask.taskOver(taskName);
                })
            });


        }
        else {
            var index = Math.floor(Math.random() * this._fail.length);


            new SyncTask(taskName, () => {
                // 第1个顺序任务逻辑
                this.playTextAnimation(this._fail[index]);
                this.once(GUI_EyesightGamePlay_W26.EVENT_PLAY_TEXT_ANIMATION_COMPLETED, this, () => {
                    SyncTask.taskOver(taskName);
                })
            });

            new SyncTask(taskName, () => {
                // 第1个顺序任务逻辑
                this.playTextAnimation("扣一点生命值!");
                this.once(GUI_EyesightGamePlay_W26.EVENT_PLAY_TEXT_ANIMATION_COMPLETED, this, () => {
                    this.hp -= 1;

                    if (this.hp <= 0) {

                    }
                    else {
                        this.startNext(0);
                    }
                    SyncTask.taskOver(taskName);
                })

            });

            new SyncTask(taskName, () => {

                if (this.hp <= 0) {
                    // 第1个顺序任务逻辑
                    this.playTextAnimation("难度这么低都过不了，真是个纯纯的废物");
                    this.once(GUI_EyesightGamePlay_W26.EVENT_PLAY_TEXT_ANIMATION_COMPLETED, this, () => {
                        
                        GameUI.dispose(this.guiID);

                        GameUI.show(15004)
                        
                        SyncTask.taskOver(taskName);

                    })
                }
                else {
                    SyncTask.taskOver(taskName);
                }
            });

        }

    }



    public set hp(value: number) {
        this._hp = value;
        this.hpLabel.text = `生命：${this._hp}`;
    }

    public get hp() {
        return this._hp;
    }

    public set lv(value: number) {
        this._lv = value;
        this.lvLabel.text = `关卡${this._lv}`;
    }

    public get lv() {
        return this._lv;
    }


}